<template>
	<div>
		<el-form label-width="80px" :model="upload.params">
			<el-form-item label="文件名称">
				<el-input v-model="upload.params.title" class="element-width" placeholder="填写文件名称" style="width: 100%;"></el-input>
			</el-form-item>

			
			<el-form-item label="备注说明">
				<el-input v-model="upload.params.remark" class="element-width" type="textarea" placeholder="填写备注说明" style="width: 100%;">
				</el-input>
			</el-form-item>
			
			<div class="displayFlexBet">
				<el-form-item label="获得时间" prop="receiveTime">
					<el-date-picker class="element-width" v-model="upload.params.receiveTime" type="date" placeholder="选择获得时间">
					</el-date-picker>
				</el-form-item>
				
				<el-form-item label="失效时间" prop="endTime">
					<el-date-picker class="element-width" v-model="upload.params.endTime" type="date" placeholder="选择失效时间">
					</el-date-picker>
				</el-form-item>
			</div>
		</el-form>

		<div style="padding: 0 36px">
			<fileUpload ref="fileUpload" @success="uploadSuccess" accept="pdf"/>
			<div style="font-size:12px;color:#999;margin-top:-20px">请选择pdf文件上传</div>
		</div>
		<div slot="footer" class="dialog-footer" style="text-align: center!important;margin-top: 20px;">
			<el-button type="primary" size="small" @click="save()" :loading="isLoading">确 定</el-button>
			<el-button size="small" @click="cancle">取 消</el-button>
		</div>


	</div>
</template>

<script>
	import $ from 'jquery'

	let that
	let APPKEY = '5da5441f62e48aedc7a3853ffc75c2db';
	let PRODID = 'gf';
	let UPLOAD_ACTION = '/upload';
	let VIEW_ACTION = '/view';
	let REMOVE_ACTION = '/remove';
	let IMGURL = 'https://www.weifeibangbang.com/wfbb-webupload/service/sys/file/upload';
	import fileUpload from "@/components/upload/fileUpload.vue";

	export default {
		name: 'app',
		components: {
			fileUpload
		},
		props: ['selectItem', 'optionType'],
		data() {
			return {
				isLoading: false,
				params: {
					categoryMainId: '',
					ticketId: '',
					itemId: ''
				},
				upload: {
					file: '',
					fileList: [],
					visible: false,
					params: {
						id: '', // 档案id
						categoryItemId: '', // 子类id
						title: '', // 标题
						fileId: '', // 文件id
						remark: '', // 注释
						endTime: '', // 到期时间
						isQualified: '1', //是否合格
						isLong: false,
						receiveTime: '',
					}
				},
			}
		},
		created: function() {},
		mounted: function() {
			that = this
			that.params.facilitatorId = that.getQueryString("facilitatorId")
			that.params.categoryMainId = that.getQueryString("categoryMainId")
			that.params.ticketId = that.$store.state.token
			that.params.itemId = that.getQueryString("itemId")

			if (that.optionType == "edit") {
				that.getEffectiveDocumentRecordByCustomerId()
			}
		},
		methods: {

			//获取最新档案列表
			getEffectiveDocumentRecordByCustomerId() {
				that.ajax({
					url: '/document/getEffectiveDocumentRecordByCustomerId?ticketId=' + that.params
						.ticketId,
					data: JSON.stringify({
						facilitatorId: that.params.facilitatorId,
						categoryItemId: that.selectItem.id,
					}),
					contentType: 'application/json',
					dataType: 'json',
					method: 'POST',
					success: function(result) {
						if (result.status == 1) {
							if (result.data) {
								that.upload.params.id = result.data.id
								that.upload.params.title = result.data.title
								that.upload.params.fileId = result.data.fileId
								that.upload.params.remark = result.data.remark
								that.upload.params.endTime = result.data.endTime
								//that.params.isQualified = '1' //是否合格
								that.upload.params.receiveTime = result.data.receiveTime
								that.upload.fileList = [{
									name: result.data.title + ".pdf",
									percentage: 0,
									raw: "File",
									size: 0,
									status: "ready",
									uid: 1667201692291,
									url: result.data.fileId
								}]
								that.$forceUpdate()
							} else {
								that.$emit("success", true)
								return that.$message({
									message: '暂未上传文件',
									type: 'warning'
								});
							}
						}
					}
				});
			},

			cancle() {
				that.$emit("cancle", true)
			},

			uploadFileOver(files, fileList) {
				return this.$message({
					message: '请上传文件数量已超出，点击删除后重新上传文件',
					type: 'warning'
				});
			},

			changeFile: function(file, fileList) {
				var _this = this;
				file.url = URL.createObjectURL(file.raw);
				if (file.raw.type.indexOf("pdf") == -1) {
					return this.$message({
						message: '请上传pdf文件',
						type: 'warning'
					});
				}
				_this.upload.fileList = fileList
				_this.upload.file = file;
			},

			removeFile() {
				this.upload.fileList = []
				this.upload.file = ''
			},

			uploadFile: function(callback) {
				var _this = this;

				if (that.optionType == "edit" && that.upload.file == '') {
					callback(that.upload.params.fileId)
				} else {
					that.$refs.fileUpload.fileQueued(_this.upload.file)

					that.$refs.fileUpload.resume(_this.upload.file)
				}
			},

			randomChar: function(l) {
				var x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
				var tmp = "";
				var timestamp = new Date().getTime();
				for (var i = 0; i < l; i++) {
					tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
				}
				return timestamp + tmp;
			},


			save() {
				let that = this

				if (that.upload.params.title == '') {
					return this.$message({
						message: '请填写标题',
						type: 'warning'
					});
				}

				that.$refs.fileUpload.resume()
			},


			uploadSuccess(fileId) {
				that.isLoading = true
				that.ajax({
					url: '/document/saveOrUpdateDocumentEntCategoryRecord',
					dataType: 'json',
					contentType: 'application/json',
					data: JSON.stringify({
						categoryItemId: that.selectItem.id,
						title: that.upload.params.title,
						remark: that.upload.params.remark,
						fileId: fileId,
						receiveTime: that.upload.params.receiveTime,
						endTime: that.upload.params.endTime,
						//isQualified: Number(that.upload.params.isQualified) ? true : false
					}),
					success: function(result) {
						that.isLoading = false
						if (result.data) {
							that.upload = that.$options.data().upload

							that.$emit("success", true)
							return that.$message({
								message: '保存成功',
								type: 'success'
							});
						} else {
							return that.$message({
								message: result.infoList[0],
								type: 'error'
							});
						}

					}
				})
			},

		},
	}
</script>

<style>
</style>